package home.projects.converter;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ChainBase;

public class Converter extends ChainBase {

   private void preProcess(Context ctx) {
      for(Object o : ctx.entrySet()) {
         Map.Entry entry = (Map.Entry)o;
         Object key = entry.getKey();
         Object val = entry.getValue();
         if(val == null) continue;
         Object convertedVal = convert(val);
         ctx.put(key, convertedVal);
      }
   }
   
   private void postProcess(Context ctx) {
      
   }
   
   private Object convert(Object o) {
      if(o instanceof List) {
         
      } else if(o instanceof Number) {
         return o.toString();
      } else if(o instanceof BigDecimal) {
         return ((BigDecimal)o).toPlainString();
      } else if(o instanceof BigInteger) {
         return o.toString();
      } else if(o instanceof Boolean) {
         return o.toString(); 
      } else if(o instanceof String) {
         return o;
      }
      
      return null;
   }
   
   public boolean execute(Context ctx) throws Exception {
      preProcess(ctx);
      super.execute(ctx);
      postProcess(ctx);
      return false;
   }

}
